package com.yonyougov.devops.cruxcisupport.appenvconfigitem;

import com.yonyougov.devops.cruxcisupport.appenv.dto.config.*;
import com.yonyougov.devops.cruxcisupport.appenv.vo.config.AppEnvConfigItemVo;
import com.yonyougov.devops.fwc.api.dto.BaseResponseDto;
import com.yonyougov.devops.fwc.core.controller.CrudController;
import com.yonyougov.devops.fwc.core.entity.Pagination;
import com.yonyougov.devops.fwc.core.service.BaseService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by ligson on 2017/8/11.
 */
@RestController
@RequestMapping(value = "/api/v2/appenvconfigitem")
public class AppEnvConfigItemController extends CrudController<AppEnvConfigItemEntity, BigInteger> {

    private final AppEnvConfigItemService appEnvConfigItemService;

    public AppEnvConfigItemController(AppEnvConfigItemService appEnvConfigItemService) {
        this.appEnvConfigItemService = appEnvConfigItemService;
    }


    @Override
    public BaseService<AppEnvConfigItemEntity, BigInteger> getService() {
        return appEnvConfigItemService;
    }

    //<!--用户自定义代码开始-->
    @PostMapping(value = "/aec/add")
    public BaseResponseDto addAppEnvConfigItem(@RequestBody @Valid AppEnvConfigItemAddRequestDto requestDto) {
        AppEnvConfigItemEntity appEnvConfigItemEntity = new AppEnvConfigItemEntity();
        BeanUtils.copyProperties(requestDto, appEnvConfigItemEntity);
        appEnvConfigItemService.add(appEnvConfigItemEntity);
        return BaseResponseDto.getSuccessInstance();
    }
    //<!--用户自定义代码结束-->

    @PostMapping(value = "/aec/update")
    public BaseResponseDto updateAppEnvConfigItem(@RequestBody @Valid AppEnvConfigItemUpdateRequestDto requestDto) {
        AppEnvConfigItemEntity appEnvConfigItemEntity = new AppEnvConfigItemEntity();
        BeanUtils.copyProperties(requestDto, appEnvConfigItemEntity);
        appEnvConfigItemService.update(appEnvConfigItemEntity);
        return BaseResponseDto.getSuccessInstance();
    }

    @PostMapping(value = "/aec/page")
    public AppEnvConfigItemQueryResponseDto findAppEnvConfigItemPage(@RequestBody AppEnvConfigItemQueryRequestDto requestDto) {
        AppEnvConfigItemEntity appEnvConfigItemEntity = new AppEnvConfigItemEntity();
        BeanUtils.copyProperties(requestDto, appEnvConfigItemEntity);
        Pagination<AppEnvConfigItemEntity> pagination = appEnvConfigItemService.findAllBy(appEnvConfigItemEntity);
        List<AppEnvConfigItemVo> appEnvConfigItemVos = new ArrayList<>();
        pagination.getDatas().forEach(configItemEntity -> {
            AppEnvConfigItemVo appEnvConfigItemVo = new AppEnvConfigItemVo();
            BeanUtils.copyProperties(configItemEntity, appEnvConfigItemVo);
            appEnvConfigItemVos.add(appEnvConfigItemVo);
        });
        return new AppEnvConfigItemQueryResponseDto(pagination.getTotalCount(), pagination.getPageCount(),
                pagination.getPageSize(), pagination.getPageNum(), appEnvConfigItemVos);
    }

    @PostMapping(value = "/aec/delete/id")
    public BaseResponseDto deleteAppEnvConfigItemById(@RequestBody @Valid AppEnvConfigItemDeleteRequestDto requestDto) {
        appEnvConfigItemService.deleteByPrimaryKey(requestDto.getId());
        return BaseResponseDto.getSuccessInstance();
    }
}
